﻿using System;
using System.Collections.Generic;
using DataLayer.Repo;
using DataLayer.Types;
using StructureMap;

namespace ViewModel.Finance
{
    public class SavingsPlanPropertiesViewModel : ItemPropertiesViewModel<SavingsPlan>
    {
        private DateTime endDate;
        private DateTime startDate;

        private decimal initialAmount;
        private decimal allotmentAmount;
        private decimal targetAmount;

        private FrequencyItem contributionFrequency;

        private bool active;
        private decimal retrievedAmount;

        private decimal balance;

        public SavingsPlanPropertiesViewModel(SavingsPlan savingsPlan)
            : base(savingsPlan)
        {
            LoadData();
        }

        private void LoadData()
        {
            name = Item.Name;
            endDate = Item.EndDate;
            startDate = Item.StartDate;
            initialAmount = Item.InitialAmount;
            targetAmount = Item.TargetAmount;
            contributionFrequency = Item.ContributionFrequency;
            active = Item.Active;
            retrievedAmount = Item.RetrievedAmount;
            PerformCalculations();
        }


        public virtual DateTime StartDate
        {
            get { return startDate; }
            set
            {
                startDate = value;
                OnPropertyChanged("StartDate");
                PerformCalculations();
            }
        }

        public virtual DateTime EndDate
        {
            get { return endDate; }
            set
            {
                endDate = value;
                OnPropertyChanged("EndDate");
                PerformCalculations();
            }
        }

        public virtual decimal AllotmentAmount
        {
            get { return allotmentAmount; }
            set
            {
                allotmentAmount = value;
                OnPropertyChanged("AllotmentAmount");
            }
        }

        public virtual decimal InitialAmount
        {
            get { return initialAmount; }
            set
            {
                initialAmount = value;
                OnPropertyChanged("InitialAmount"); 
                PerformCalculations();
            }
        }

        public virtual decimal TargetAmount
        {
            get { return targetAmount; }
            set
            {
                targetAmount = value;
                OnPropertyChanged("TargetAmount");
                PerformCalculations();
            }
        }

        public FrequencyItem ContributionFrequency
        {
            get { return contributionFrequency; }
            set
            {
                contributionFrequency = value;
                OnPropertyChanged("ContributionFrequency"); 
                PerformCalculations();
            }
        }

        public virtual bool Active
        {
            get { return active; }
            set { active = value; OnPropertyChanged("Active"); }
        }

        public virtual decimal RetrievedAmount
        {
            get { return retrievedAmount; }
            set
            {
                retrievedAmount = value; 
                OnPropertyChanged("RetrievedAmount");
                PerformCalculations();
            }
        }

        public decimal Balance
        {
            get { return balance;}
            set { balance = value; OnPropertyChanged("Balance"); }
        }

        public ICollection<FrequencyItem> FrequencyItems
        {
            get { return ObjectFactory.GetInstance<IRepository<FrequencyItem>>().FindAll(); }
        }

        private void PerformCalculations()
        {
            AllotmentAmount = SavingsPlanCalculator.CalculateAllotmentAmount(StartDate, EndDate, TargetAmount,
                                                                             InitialAmount, ContributionFrequency);
            Balance = SavingsPlanCalculator.CalculateBalance(StartDate, EndDate, DateTime.Today, TargetAmount, InitialAmount, RetrievedAmount);
        }

        private void CollectData()
        {
            Item.Name = name;
            Item.StartDate = startDate;
            Item.EndDate = endDate;
            Item.InitialAmount = initialAmount;
            Item.TargetAmount = targetAmount;
            Item.ContributionFrequency = contributionFrequency;
            Item.Active = active;
            Item.RetrievedAmount = retrievedAmount;
        }

        /// <summary>
        /// Collects the form data into the BudgetItem object and then saves it to the database.
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            CollectData();
            Item.Save();
            return true;
        }
    }
}
